version: "3.8"

services:
  db:
    container_name: db
    image: postgres:latest
    environment:
      POSTGRES_DB: service
      POSTGRES_USER: ${POSTGRES_USER}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
      POSTGRES_PORT: ${POSTGRES_PORT}
      PGDATA: /var/lib/postgresql/data/pgdata
    volumes:
      - ./app_data/pgdata:/var/lib/postgresql/data/pgdata
    healthcheck:
      test: pg_isready -U ${POSTGRES_USER} -d ${POSTGRES_DB_NAME}
      interval: 10s
      timeout: 30s
      retries: 5
      start_period: 30s
    restart: unless-stopped
    ports:
      - 5432:${POSTGRES_PORT}
    networks:
      - backend

  users-db:
    container_name: users-db
    image: postgres:latest
    environment:
      POSTGRES_DB: ${AUTH_POSTGRES_DB_NAME}
      POSTGRES_USER: ${AUTH_POSTGRES_USER}
      POSTGRES_PASSWORD: ${AUTH_POSTGRES_PASSWORD}
      POSTGRES_PORT: ${AUTH_POSTGRES_PORT}
      PGDATA: /var/lib/postgresql/data/pgdata
    volumes:
      - ./app_data/auth_pgdata:/var/lib/postgresql/data/pgdata
    healthcheck:
      test: pg_isready -U ${AUTH_POSTGRES_USER} -d ${AUTH_POSTGRES_DB_NAME}
      interval: 10s
      timeout: 30s
      retries: 5
      start_period: 30s
    restart: unless-stopped
    command: -p ${AUTH_POSTGRES_PORT}
    expose:
      - ${AUTH_POSTGRES_PORT}
    ports:
      - ${AUTH_POSTGRES_PORT}:${AUTH_POSTGRES_PORT}
    networks:
      - backend

  api:
    container_name: api
    build:
      context: ./
      dockerfile: ./backend/Dockerfile
    env_file:
      - .env
    volumes:
      - ./commons:/opt/app/commons
      - ./backend:/opt/app/
      - ./staticfiles:/opt/app/staticfiles
    command: bash -c "python start.py"
    depends_on:
      - db
    ports:
      - 8000:${API_PORT}
    networks:
      - backend

  auth:
    container_name: auth
    volumes:
      - ./auth/src:/app
    env_file: .env
    build:
      context: ./
      dockerfile: ./auth/Dockerfile
    depends_on:
      users-db:
        condition: service_healthy
      limiter:
        condition: service_started
    expose:
      - 8001
    networks:
      - backend

  image_core:
    container_name: image_core
    volumes:
      - ./image_core/src:/app
      - ./app_data/datasets:/app/datasets
      - ./downloads:/app/downloads
      - ./staticfiles:/app/arts
    env_file:
      - .env
    build:
      context: ./
      dockerfile: ./image_core/Dockerfile
    ports:
      - 8012:8012
    networks:
      - backend

  text_core:
    container_name: text_core
    volumes:
      - ./text_core:/app
    env_file:
      - .env
    build:
      context: ./
      dockerfile: ./text_core/Dockerfile
    command: bash -c "uvicorn main:app --reload --workers 1 --host 0.0.0.0 --port 8000"
    ports:
      - 8003:8000
    networks:
      - backend

  limiter:
    image: redis:latest
    command: /bin/sh -c "redis-server --requirepass $$REDIS_PASSWORD"
    volumes:
      - limiter:/data
    env_file:
      - .env
    expose:
      - 6379
    healthcheck:
      test: [ "CMD", "redis-cli", "--raw", "incr", "ping" ]
    networks:
      - backend

  front:
    container_name: front
    build:
      context: ./
      dockerfile: ./frontend/Dockerfile
    env_file:
      - .env
    depends_on:
      - api
    environment:
      - REACT_APP_SERVER_API:${REACT_APP_SERVER_API}
    ports:
      - ${FRONTEND_PORT}:3000
    networks:
      - frontend

  nginx-proxi-pass:
    restart: always
    build:
      context: .
      dockerfile: nginx/Dockerfile
    depends_on:
      - auth
    ports:
      - 80:80
    networks:
      - backend
      - frontend

networks:
  backend:
    driver: bridge
  frontend:
    driver: bridge

volumes:
  limiter:
    driver: local
  dbdata:
    driver: local
